package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.vo.CountByTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;


    @Override
    public Integer getCancelledReservationCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Long userId = UserThreadLocal.getUserId();
        int count = (int) count(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, userId)
                .between(Reservation::getUpdateTime, startTime, endTime)
        );
        return count;
    }

    /**
     * 获取预约时间段内剩余预约的次数
     * @param time
     * @return
     */
    @Override
    public List<CountByTime> getCountByTime(long time) {
        // 将时间戳转换为LocalDateTime
        LocalDateTime queryTime = LocalDateTimeUtil.of(time);
        // 获取查询日期的开始时间和结束时间
        LocalDateTime startTime = queryTime.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        // 查询指定日期内所有有效的预约记录(状态为0)
        List<Reservation> reservations = list(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 0)
                .between(Reservation::getTime, startTime, endTime));
        // 定义每个半小时段的最大预约数
        final int MAX_RESERVATIONS_PER_SLOT = 6;
        // 创建结果列表
        List<CountByTime> result = new ArrayList<>();
        // 生成一天中所有的半小时时间段 (00:00, 00:30, 01:00, ..., 23:30)
        for (int hour = 0; hour < 24; hour++) {
            for (int minute = 0; minute < 60; minute += 30) {
                // 构造当前时间段的开始时间
                LocalDateTime slotStart = startTime.withHour(hour).withMinute(minute);
                LocalDateTime slotEnd = slotStart.plusMinutes(30);
                // 统计当前时间段内的预约数量
                int reservationCount = 0;
                for (Reservation reservation : reservations) {
                    LocalDateTime reservationTime = reservation.getTime();
                    if (reservationTime != null &&
                            !reservationTime.isBefore(slotStart) &&
                            reservationTime.isBefore(slotEnd)) {
                        reservationCount++;
                    }
                }
                // 计算剩余预约次数
                int remainingCount = MAX_RESERVATIONS_PER_SLOT - reservationCount;
                // 创建CountByTime对象并添加到结果列表
                CountByTime countByTime = new CountByTime();
                countByTime.setTime(slotStart.toString());
                countByTime.setCount(remainingCount);
                result.add(countByTime);
            }
        }
        return result;
    }

    @Override
    public int add(Reservation reservation) {
        reservation.setStatus(0);
        boolean save = save(reservation);
        return save ? 1 : 0;
    }

    @Override
    public List<Reservation> findAll(Integer status) {
        List<Reservation> list = list(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getStatus, status));
        return list;
    }

    @Override
    public boolean cancel(Long id) {
        boolean result = remove(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getId, id));
        return result;
    }
}
